class Person < ActiveRecord::Base
  has_attached_file :avatar,
    :url => "/system/:attachment/:id/:style/:id.:extension",
    :path => ":rails_root/public/system/:attachment/:id/:style/:id.:extension",
    :default_url => "/images/missing.png",
    :default_style => :thumb,
    :styles => {:thumb => "100x100#", :small  => "150x150>", :medium => "200x200" }

  has_one :user, :through => :user_person
  has_one :student, :foreign_key => 'id'
  has_one :teacher, :foreign_key => 'id'
  has_one :system_operator, :foreign_key => 'id'

  has_many :contact_infos

  validates_presence_of :name, :patronymic, :matronymic, :dob
  validate :dob_is_future
  validates_attachment_content_type :avatar, 
    :content_type =>  ["image/jpeg", "image/png", "image/gif", "image/x-png", "image/pjpeg"],
    :message => "El formato del archivo es incorrecto. Por favor asegurese de que sea una imagen."
  # TODO: add as system parameter
  validates_attachment_size :avatar, :less_than => 2.megabytes

  
  def reverse_full_name
    "#{self[:patronymic]} #{self[:matronymic]} #{self[:name]} #{self[:middle_name]}"
  end

  def full_name
    "#{self[:name]} #{self[:middle_name]} #{self[:patronymic]} #{self[:matronymic]}"
  end

  protected
  def dob_is_future
    errors.add(:dob, 'La fecha de nacimiento debe de ser anterior al día de hoy.') if dob.nil? ||
      dob >= Date.today
  end
end
